package com.wt.struggle.goodbye.user.activity

import android.content.Intent
import android.os.Bundle
import android.support.design.widget.TabLayout
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import com.google.gson.reflect.TypeToken
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.Config
import com.wt.struggle.goodbye.base.ProActivity
import com.wt.struggle.goodbye.base.Send
import com.wt.struggle.goodbye.base.Share
import com.wt.struggle.goodbye.info.NetInfo
import com.wt.struggle.goodbye.info.NewsInfo
import com.wt.struggle.goodbye.sq.adapter.PickUserAdapter
import kotlinx.android.synthetic.main.delete_recycle_layout.*
import kotlinx.android.synthetic.main.head.*

/**
 * 删除回收站记录
 */
class DeleteRecycleActivity : ProActivity() {

    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {
        when (what) {

            Send.DELETE_TIE -> {

                if (netInfo.code == Config.SUCCESS) {
                    val result = netInfo.dataResult
                    if (result != null && result != "" && result != "null") {
                        val arr = gson!!.fromJson<ArrayList<NewsInfo>>(
                            result,
                            object : TypeToken<ArrayList<NewsInfo>>() {}.type
                        )
                        if (arr.size != 0) {
                            adapter.updateDataClear(arr)
                        } else {
                            adapter.updateDataClear(arrayListOf())
                            showToastShort("暂无数据")
                        }

                    }

                } else {
                    showToastShort("暂无数据")
                    adapter.updateDataClear(arrayListOf())
                }
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.delete_recycle_layout)
        tvTitle.text = "删除回收站"
        tvRight.text = "删帖恢复"
        imageBack.setOnClickListener {
            finish()
        }

        tvRight.setOnClickListener {
            startActivity(Intent(this, SiXinOrRecoverActivity::class.java))
        }

        initAdapter()

        getRecycleList()

    }

    private fun initTabLayout() {
        tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                when (tab!!.position) {
                    0 -> type = 2
                    1 -> type = 3
                }

                getRecycleList()

            }

        })
    }


    /**
     *  3 为自己删帖
     */
    var type = 3

    var page = 1

    /**
     * 获取回收站列表
     */
    private fun getRecycleList() {
        send!!.getRecycleList(page, type, Share.getId(this), handler)
    }

    val list = arrayListOf<NewsInfo>()

    lateinit var adapter: PickUserAdapter

    private fun initAdapter() {
        recycleRecyclerView.layoutManager = LinearLayoutManager(this)
        recycleRecyclerView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
        adapter = PickUserAdapter(this, list)
        adapter.isEnterCode = 5
        recycleRecyclerView.adapter = adapter
    }
}